package EGS.impl;
import java.util.concurrent.ExecutorService;

import org.dom4j.Document;
import org.dom4j.Element;
import TOOLS.SQLtool;
import TOOLS.errCodeManagement;
import WORKPLACE.stationClient;

public class EGS_execute_service10021Impl{
	public boolean isUnknowCase = false;
	ExecutorService iExecutorService;
	Document MSG;
	boolean isPanel = false;
	stationClient iClient=null;
	SQLtool sql = null;
	/**
	 * 10021 人员信息查询
	 */
	public EGS_execute_service10021Impl(stationClient nClient, Document MSG_in, ExecutorService executorService, 
			SQLtool goldenSql) throws Exception {
		super();
		sql=goldenSql;
		if(nClient!=null) {
			iClient=nClient;
		}else {
			iClient=null;
		}
		MSG = MSG_in;
		iExecutorService = executorService;
	}

	public Document runCal() {
		String msgClass = "";
		try {
			if (MSG == null) {
				return null;
			}
			Element Mroot = MSG.getRootElement();
			msgClass = Mroot.element("header").attribute("messageClass").getValue();
			String transactionID = Mroot.element("header").attribute("transactionID").getValue();
			String iStation = Mroot.element("header").element("location").attribute("equipmentName").getValue();

			if (msgClass.equals("10021")) {// TODO 人员信息查询
				String userID =Mroot.element("body").element("user").attribute("number").getValue();
				Mroot.element("body").remove(Mroot.element("body").element("user"));
				Element eleR = Mroot.element("body").addElement("result");
				Element eleA = Mroot.element("body").addElement("authority");
				Element eleU = Mroot.element("body").addElement("user");
				
				try {
					//TODO 功能转移中，临时回复固定值，含义是技能等级，数字大的覆盖数字小的能力
					eleA.addAttribute("value", "4");
					eleA.addAttribute("starttime", "2012-01-01 00:00:00 000");//iFormat="yyyy-MM-dd HH:mm:ss SSS";
					eleA.addAttribute("endtime", "2112-01-01 00:00:00 000");
					eleA.addAttribute("alarmtime", "2112-01-01 00:00:00 000");
					eleU.addAttribute("number", userID);
					eleU.addAttribute("name", "功能转移中");
					eleU.addAttribute("state", "01");
					//<!--state =01enrollment：在岗 02resignation:离职-->
					
					
					eleR.addAttribute("errorCode", "0");
					eleR.addAttribute("errorText", "Valid");
					eleR.addAttribute("actionCode", "0");
					AKSLogs(100211, "" + iStation + "_" + userID);
				} catch (Exception e) {
					String str = errCodeManagement.getErrText(e.getMessage());
					eleR.addAttribute("errorCode", "1");
					eleR.addAttribute("errorText", str);
					eleR.addAttribute("actionCode", transactionID);
					AKSLogs(100210, "" + iStation + "_" + userID);
				}
			} else {
				isUnknowCase = true;
				return null;
			}
			return MSG;
		} catch (Exception e) {
			if(iClient==null) {
				Element Mroot = MSG.getRootElement();
				Element eleR = Mroot.element("body").addElement("result");
				eleR.addAttribute("errorCode", "1");
				eleR.addAttribute("errorText", "工位信息获取失败");
				eleR.addAttribute("actionCode", "0");
				return MSG;
			}
			String str = errCodeManagement.getErrText(e.getMessage());
			System.out.println(str);
			return null;
		}

	}

	public void AKSLogs(int iType, String ilogClientNews) {
		SQLtool.AKSLogs(iType, ilogClientNews);
	}

}
